package com.base.cn.platform.os.common.filter;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;
import java.util.*;
import java.util.stream.IntStream;

/**
 * 请求过滤器
 *
 * @author s.li
 * @date 2019/06/01
 */
@Component
@WebFilter(urlPatterns = "/*", filterName = "requestFilter")
@Slf4j
public class RequestFilter implements Filter {


    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) res;
        RequestData requestData = new RequestData();

        //所有的头元素
        Enumeration<String> header = request.getHeaderNames();
        Map<String,String> headerMap = new HashMap<>();
        while (header.hasMoreElements()){
            String name = header.nextElement();
            String value = request.getHeader(name);
            headerMap.put(name,value);
        }
        requestData.setHeaderMap(headerMap);


        Cookie[] cookies = request.getCookies();
        if(cookies !=null && cookies.length > 0){
            Cookie[] _cookies = new Cookie[cookies.length];
            IntStream.range(0,cookies.length).forEach(index->{
                Cookie cookie = cookies[index];
                Cookie _cookie = new Cookie(cookie.getName(),cookie.getValue());
                _cookie.setPath(cookie.getPath());
                _cookie.setMaxAge(cookie.getMaxAge());
                if(cookie.getDomain() !=null){
                    _cookie.setDomain(cookie.getDomain());
                }
                _cookie.setSecure(cookie.getSecure());
                _cookie.setComment(cookie.getComment());
                _cookie.setVersion(cookie.getVersion());
                _cookie.setHttpOnly(cookie.isHttpOnly());
                _cookies[index] = _cookie;
            });
            requestData.setCookies(_cookies);
        }

        Map<String,String[]> parameterMap = new HashMap<>();
        Enumeration<String> parameterNames = request.getParameterNames();
        while (parameterNames.hasMoreElements()){
            String name = parameterNames.nextElement();
            String[] value = request.getParameterValues(name);
            parameterMap.put(name,value);
        }
        requestData.setParameterMap(parameterMap);


        String requestURI = request.getRequestURI();
        requestData.setRequestURI(requestURI);

        StringBuffer requestRUL = request.getRequestURL();
        requestData.setRequestRUL(new StringBuffer(requestRUL.toString()));

        String remoteAddr = request.getRemoteAddr();
        requestData.setRemoteAddr(remoteAddr);

        RequestData.requestDataThreadLocal.set(requestData);

        chain.doFilter(request,response);
    }

    @Override
    public void destroy() {

    }
}
